/*  This file is part of -_-.

    -_- is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    -_- is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with -_-.  If not, see <http://www.gnu.org/licenses/>.

    Copyright 2012-2013 Oliver Katz */

#ifndef __COMMON_MSG_H
#define __COMMON_MSG_H

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

//struct z_log: a message log (can write to file or stdout or anything)
typedef struct
{
	FILE *f; //memberof z_log: file handle
} z_log;

z_log z_log_init(char *p); //methodof z_log: initializer
//argof z_log_init: char *p: path of the file to output to
z_log z_log_init_with_file(FILE *f); //methodof z_log: initializer with a specific file handle (like stdout)
int z_log_release(z_log l); //methodof z_log: uninitializer
FILE *z_log_file_get(z_log *l); //methodof z_log: get file handle

typedef struct tm *z_time;

z_time z_now();
char *z_timestr(z_time t);

//struct z_msg: a message that can be logged
typedef struct
{
	z_time time; //memberof z_msg: time info of message
	int code; //memberof z_msg: the error code of the message (anything less than 0 will kill the process)
	char *msg; //memberof z_msg: the message to display
} z_msg;

z_msg z_msg_init(int c, char *m); //methodof z_msg: initializer
//argof z_msg_init: int c: the error code
//argof z_msg_init: char *m: the message to display
int z_msg_release(z_msg m); //methodof z_msg: uninitializer
int z_msg_dump(z_log *l, z_msg *m); //methodof z_msg: display message on log 'l'
int z_msg_eval(z_msg *m); //methodof z_msg: evaluate error code (kills process if it's less than 0)

/// If you need to log messages, here's the place to do it.
/// Here's how:
/// {{z_log log = z_log_init_with_file(stdout);
///
/// // now that we've initialized the log, let's log a message
/// z_msg msg = z_msg_init(0, "this won't quit the program");
/// z_msg_dump(&log, &msg);
/// z_msg_eval(&msg);
/// z_msg_release(msg);
///
/// // next let's log a fatal error
/// msg = z_msg_init(-1, "this will quit the program");
/// z_msg_dump(&log, &msg);
/// z_msg_eval(&msg);
/// z_msg_release(msg);
///
/// z_log_release(log);}}

#endif
